# FrontendWindow - GTK-window that contains user interface and some toolbars
# Copyright (C) 2007 Lauri Taimila
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2007, Lauri Taimila"
__author__ = "Lauri Taimila <lauri@taimila.com>"

import os
import gtk
import pygtk
import gtk.glade

# Dialogs
from utils.log_viewer import LogViewer
from utils.preferences_dialog import PreferencesDialog
from utils.content_management_dialog import ContentManagementDialog

class FrontendWindow:
    """
    Frontend GTK-window
    
    This is a main window of the Entertainer frontend. This window contains
    menus, toolbar, statusbar and the user interface itself.
    """
    
    # Glade widget container
    __widgets = None
    
    # Window object
    __window = None
    
    # Frontend object
    __frontend = None
    
    # Clutter widget
    __clutter_widget = None
    
    # Flag for fullscreen
    __is_fullscreen = False
    
    def __init__(self, frontend):
        """
        Initialize window.
        @param frontend: Frontend object
        """
        self.__frontend = frontend
        gladefile = "frontend/glade/entertainer-window.glade"
        self.__widgets = gtk.glade.XML(gladefile)
        self.__window = self.__widgets.get_widget("window")
        callback_dict = { "on_toolbutton_fullscreen_clicked" : self.toggle_fullscreen,
                          "on_toolbutton_content_clicked" : self.on_toolbutton_content_clicked,
                          "on_toolbutton_preferences_clicked" : self.on_toolbutton_preferences_clicked,
                          "on_toolbutton_log_clicked" : self.on_toolbutton_log_clicked,
                          "on_menu_preferences_activate" : self.on_toolbutton_preferences_clicked,
                          "on_menu_content_activate" : self.on_toolbutton_content_clicked,
                          "on_menu_log_viewer_activate" : self.on_toolbutton_log_clicked,
                          "on_menu_quit_activate" : self.on_menu_quit_activate,
                          "on_menu_show_menu_toggled" : self.on_menu_show_menu_toggled,
                          "on_menu_show_toolbar_toggled" : self.on_menu_show_toolbar_toggled,
                          "on_menu_show_statusbar_toggled" : self.on_menu_show_statusbar_toggled,
                          "on_menu_fullscreen_activate" : self.toggle_fullscreen,
                          "on_menu_about_activate" : self.on_menu_about_activate,
                          "on_window_key_press_event" : self.on_window_key_press_event }
        self.__widgets.signal_autoconnect(callback_dict)
        
    def setClutterWidget(self, clutter_widget):
        """
        Embed Clutter UI to GTK Window.
        @param clutter_widget: ClutterGTK Widget that is displayd in window.
        """
        self.__clutter_widget = clutter_widget
        self.__clutter_widget.connect('size-allocate', self.sizeChangedCallback)
        container = self.__widgets.get_widget("main_vbox")
        container.add(clutter_widget)
        container.reorder_child(clutter_widget, 2)
        clutter_widget.show()
    
    def getClutterWidget(self):
        """
        Get clutter widget
        @return Clutter embedded GTK-widget
        """
        return self.__clutter_widget
    
    def sizeChangedCallback(self, widget, event):
        """
        Window size has changed. We have to update stage size.
        @param widget: Widget object (not needed)
        @param event: Event object (not needed)
        """
        width = self.__clutter_widget.get_allocation().width
        height = self.__clutter_widget.get_allocation().height
        self.__frontend.getUserInterface().resizeStage(width, height)
        
    def show(self):
        """Show window"""
        self.__window.show()
        
    def hide(self):
        """Hide window"""
        self.__window.hide()
        
    def on_window_key_press_event(self, widget, event):
        if event.keyval == gtk.keysyms.m: # and event.state == gtk.ModifierType.GDK_CONTROL_MASK:
            menubar = self.__widgets.get_widget("menubar")
            if menubar.get_property('visible'):
                menubar.hide()
            else:
                menubar.show()
        else:
            #FIXME: Is this safe to use GTKEvents as ClutterEvents?! Nope :)
            #       Waiting for clutter team to include tranformation function.
            self.__frontend.getUserInterface().handle_key_press_event(None, event)
        
    def toggle_fullscreen(self, widget):
        """
        Toggle fullscreen/window mode.
        @param widget: Event source widget (Isn't needed really)
        """
        menubar = self.__widgets.get_widget("menubar")
        toolbar = self.__widgets.get_widget("toolbar")
        statusbar = self.__widgets.get_widget("statusbar")

        if not self.__is_fullscreen:
            menubar.hide()
            toolbar.hide()
            statusbar.hide()
            self.__window.set_keep_above(True)
            self.__window.fullscreen()
        else:
            #FIXME: This is not a good way probably...
            menu = self.__widgets.get_widget("menu_show_menu").get_active()
            tool = self.__widgets.get_widget("menu_show_toolbar").get_active()
            status = self.__widgets.get_widget("menu_show_statusbar").get_active()
            if menu:
                menubar.show()
            if tool:
                toolbar.show()
            if status:
                statusbar.show()
            self.__window.set_keep_above(False)
            self.__window.unfullscreen()
        self.__is_fullscreen = not self.__is_fullscreen
        
    def on_toolbutton_content_clicked(self, widget):
        """Show content management dialog."""
        ContentManagementDialog(False)
        
    def on_toolbutton_preferences_clicked(self, widget):
        """Show preferences dialog."""
        PreferencesDialog(False)
        
    def on_toolbutton_log_clicked(self, widget):
        """Show log viewer dialog."""
        LogViewer(False)
        
    def on_menu_quit_activate(self, widget):
        """Quit selected from File menu"""
        self.__frontend.quit_frontend()
        
    def on_menu_show_menu_toggled(self, widget):
        """Toggle show/hide of the menubar"""
        menubar = self.__widgets.get_widget("menubar")
        active = self.__widgets.get_widget("menu_show_menu").get_active()
        if active:
            menubar.show()
        else:
            menubar.hide()
        
    def on_menu_show_toolbar_toggled(self, widget):
        """Toggle show/hide of the toolbar"""
        toolbar = self.__widgets.get_widget("toolbar")
        active = self.__widgets.get_widget("menu_show_toolbar").get_active()
        if active:
            toolbar.show()
        else:
            toolbar.hide()
        
    def on_menu_show_statusbar_toggled(self, widget):
        """Toggle show/hide of the statusbar"""
        statusbar = self.__widgets.get_widget("statusbar")
        active = self.__widgets.get_widget("menu_show_statusbar").get_active()
        if active:
            statusbar.show()
        else:
            statusbar.hide()
        
    def on_menu_about_activate(self, widget):
        """Show about dialog"""
        about = self.__widgets.get_widget("aboutdialog")
        about.run()
        about.hide()
